#include "searchTest.h"
#include "Search.h"
#include "util.h"
#include <CRTDBG.h>
#include <iostream>
#include "basicSort.h"

#ifdef _DEBUG
typedef int (*search_func)(int a[], int n, int v, int *cnt);
#else
typedef int (*search_func)(int a[], int n, int v);
#endif

struct search_func_info
{
    search_func func;
    const char *desc;
    const char *extra;
};

static struct search_func_info funs[] = 
{
    {binarySearch,      "simplest", "std"},
    {binarySearch2,     "simplest", "recursive"},
    {binarySearchVar1,  "Variant1", "std"},
};

int generate_test_array(int **arr, int sz, bool tips, bool sorted, bool tips_sorted)
{
    int  *a = nullptr;
    int ret;

    a = get_array_mem(sz);
    _ASSERTE(a != nullptr);


    ret = gen_array(a, sz);
    _ASSERTE(ret == 0);

    if(tips)
    {
        std::cout << "array: " << std::endl;
        prt_array(a, sz);
    }
    
    if(sorted)
    {
        insertSort1(a, sz);
    }

    if(tips_sorted)
    {
        std::cout << "sorted: " << std::endl;
        prt_array(a, sz);
    }

    *arr = a;

    return 0;
}

void func_test(int ix, int sz)
{
    //int sz = 100;
    int *a = nullptr;
    int v = 0, r;
#ifdef _DEBUG
    int s_cnt;
#endif
    int n = sizeof(funs)/sizeof(funs[0]);

    search_func f = nullptr;


    if(ix >= n)
        return;

    generate_test_array(&a, sz, true, true, true);

    
    std::cout << "enter a integer for search in array: " << std::endl;
    std::cin >> v;
    std::cout << "your typed num: " << v << std::endl;

    

    f = funs[ix].func;
#ifdef _DEBUG
    r = f(a, sz, v, &s_cnt);
#else
    r = f(a, sz, v);
#endif

    std::cout << "binary search(" << funs[ix].desc << "--" << funs[ix].extra 
        << ")" << "for val: " << v
        << " at ix(if -1 no this v): " << r 
        << std::endl;

    destroy_array_mem(a);
}


void search_test(void)
{
    int i;

#if 0
    for(i = 0; i < (int)(sizeof(funs)/sizeof(funs[0])); i++)
    {
        func_test(i, 100);        
    }
#else
    for(i = 0; i < 5; i++)
    {
        func_test(2, 100);
    }
#endif
}
